﻿package {
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;

//	import cmodule.JPEGEncoder.CLibInit;
	import com.adobe.images.JPGEncoder;
	import org.bytearray.JPEGEncoder;
	import lu.kyle.JPGEncoder;

	[SWF(width="1000", height="600")]
	public class JPEGEncoderTest extends Sprite {
		private var loader:Loader;
		public function JPEGEncoderTest() {
			super();

			stage.scaleMode = StageScaleMode.NO_SCALE;

			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadImage);
			loader.load(new URLRequest("screen_sizes.png"));
		}
		private function onLoadImage(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onLoadImage);

			var bmp:BitmapData = new BitmapData(stage.stageWidth>>1, stage.stageHeight>>1, false);
			bmp.draw(loader);

			var tstart:int;
			var jpg:ByteArray;
			var ldr:Loader;

			var encoderA:com.adobe.images.JPGEncoder = new com.adobe.images.JPGEncoder(80);
			tstart = getTimer();
			jpg = encoderA.encode(bmp);
			trace(getTimer()-tstart, "ms");
			ldr = new Loader();
			ldr.loadBytes(jpg);
			addChild(ldr);

			//var lib:CLibInit = new CLibInit;
			//encoder = lib.init();
			//var pixels:ByteArray = bmp.getPixels(bmp.rect);
			//pixels.position = 0;
			//trace(bmp.width, bmp.height, pixels.length);
			//tstart = getTimer();
			//jpg = encoder.encode(pixels, bmp.width, bmp.height);
			//trace(getTimer()-tstart, "ms");

			var encoderB:org.bytearray.JPEGEncoder = new org.bytearray.JPEGEncoder(80);
			tstart = getTimer();
			jpg = encoderB.encode(bmp);
			trace(getTimer()-tstart, "ms");
			ldr = new Loader();
			ldr.x = stage.stageWidth>>1;
			ldr.loadBytes(jpg);
			addChild(ldr);

			var encoderK:lu.kyle.JPGEncoder = new lu.kyle.JPGEncoder(80);
			tstart = getTimer();
			jpg = encoderK.encode(bmp);
			trace(getTimer()-tstart, "ms");
			ldr = new Loader();
			ldr.y = stage.stageHeight>>1;
			ldr.loadBytes(jpg);
			addChild(ldr);

			bmp.dispose();
		}
	}
}
